package com.mofeng.myshop.until;

import com.mofeng.myshop.exception.MyException;
import io.github.eternalstone.captcha.gp.base.Captcha;
import io.github.eternalstone.captcha.gp.base.CaptchaEnum;
import io.github.eternalstone.captcha.gp.base.TextEntry;
import io.github.eternalstone.captcha.gp.captcha.ArithmeticCaptcha;
import io.github.eternalstone.captcha.gp.captcha.GifCaptcha;
import io.github.eternalstone.captcha.gp.captcha.SpecCaptcha;
import io.github.eternalstone.captcha.gp.factory.CaptchaFactory;
import io.github.eternalstone.captcha.gp.factory.CaptchaProperty;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.awt.*;
import java.util.Random;

import static io.github.eternalstone.captcha.gp.base.Captcha.*;


/**
 * 验证码相关的工具类 官网 https://gitee.com/eternalstone/EasyCaptchaBoot
 */
public class CaptchaCodeUtil {
    private static final int WIDTH = 200;
    private static final int HEIGHT = 40;
//    private static final int CAPTCHA_LEN = 6;
//    private static final int OPERATION_LEN = 3;
    private static final int CAPTCHA_LEN = 5;
    private static final int OPERATION_LEN = 2;
    private static final int FONT_SIZE = 40;
    private static final Random rm = new Random();

    //    TYPE_DEFAULT	数字和字母混合 TYPE_ONLY_NUMBER	纯数字
    //    TYPE_ONLY_CHAR	纯字母 TYPE_ONLY_UPPER	纯大写字母
    //    TYPE_ONLY_LOWER	纯小写字母 TYPE_NUM_AND_UPPER	数字和大写字母
    private static final int[] captchaCharType = {TYPE_DEFAULT, TYPE_ONLY_NUMBER,
            TYPE_ONLY_CHAR, TYPE_ONLY_UPPER, TYPE_ONLY_LOWER, TYPE_NUM_AND_UPPER};
    private static final int[] captchaFont = {Captcha.FONT_1,
            Captcha.FONT_2, Captcha.FONT_3, Captcha.FONT_4,
            Captcha.FONT_5, Captcha.FONT_6, Captcha.FONT_7,
            Captcha.FONT_8, Captcha.FONT_9, Captcha.FONT_10};

    public static String code() {
        try {
            HttpServletResponse response = getHttpServletResponse();
            Captcha captcha = CaptchaFactory.getCaptcha(getCaptcha());//创建验证码实例对象
            TextEntry text = captcha.createText();
            captcha.out(response.getOutputStream(), text);
            return text.getKey().toLowerCase();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取设置好的response对象=
     */
    private static HttpServletResponse getHttpServletResponse() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert requestAttributes != null;
        HttpServletResponse response = requestAttributes.getResponse();
        // 设置请求头为输出图片类型
        assert response != null;
        response.setContentType("image/gif");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        return response;
    }

    /**
     * 获取实体的配置对象
     */
    private static CaptchaProperty getCaptcha() {
        CaptchaProperty property = new CaptchaProperty();
        property.setWidth(WIDTH);//配置验证码宽度
        property.setHeight(HEIGHT);//配置验证码高度
        property.setCharType(captchaCharType[rm.nextInt(captchaCharType.length)]);//配置验证码字符类性，纯数字、纯字母、字母数字混合
        property.setFont(captchaFont[rm.nextInt(captchaFont.length)]);//配置内置字体
        property.setLength(CAPTCHA_LEN);//配置验证码几个字符
        setCaptchaType(property);
        return property;
    }

    /**
     * 设置验证码类型
     */
    private static void setCaptchaType(CaptchaProperty property) {
//        int captchaType = rm.nextInt(5);
        int captchaType = rm.nextInt(2);
        CaptchaEnum type;
        switch (captchaType) {
            case 0:
                type = CaptchaEnum.SPEC;//普通字符验证码
                break;
            case 1:
                type = CaptchaEnum.ARITHMETIC;//算术验证码
                //Captcha.FONT_8  FONT_3  算术字体8 和 3会乱码
                int i = captchaFont[rm.nextInt(captchaFont.length)];
                while (i == Captcha.FONT_8 || i == Captcha.FONT_3){
                    i = captchaFont[rm.nextInt(captchaFont.length)];
                }
                property.setFont(i);//配置内置字体
                property.setLength(OPERATION_LEN);//算术三位算
                break;
            case 2:
                type = CaptchaEnum.GIF;//GIF动图验证码
                break;
            case 3:
                type = CaptchaEnum.CHINESE;//中文验证码
                property.setLength(OPERATION_LEN);//中文三位
                property.setFont(new Font("楷体", Font.PLAIN, FONT_SIZE));//设置系统字体 防止中文乱码
                break;
            default:
                type = CaptchaEnum.CHINESE_GIF;//中文GIF动图验证码
                property.setLength(OPERATION_LEN);//中文动态三位
                property.setFont(new Font("楷体", Font.PLAIN, FONT_SIZE));//设置系统字体 防止中文乱码
                break;
        }
        property.setCaptcha(type);
    }
}
